package com.huanzhidadi.java8inaction.chapter4;

import com.huanzhidadi.java8inaction.model.Dish;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@Slf4j
public class NumberStreamTest {
    public static void main(String[] args) {

        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH)
        );

        Integer calories1 = menu.stream()
                .map(Dish::getCalories)
                .reduce(0, Integer::sum);
        log.info("reduce: " + calories1);

        /*Integer calories2 = menu.stream()
                .map(Dish::getCalories)
                .sum();*/

        Integer calories2 = menu.stream()
                .mapToInt(Dish::getCalories)
                .sum();
        log.info("mapToInt sum: " + calories2);

        Integer caloriesTop = menu.stream()
                .mapToInt(Dish::getCalories)
                .max()
                // max方法返回的是一个OptionalInt类型对象，这是一个表示值存在或不存在的容器
                .getAsInt();
        log.info("mapToInt max: " + caloriesTop);

        // 转换为对象流
        IntStream intStream = menu.stream()
                .mapToInt(Dish::getCalories);
        Stream<Integer> stream = intStream.boxed();

        // 数值范围
        IntStream evenNum1 = IntStream.rangeClosed(1, 100)
                .filter(n -> n % 2 == 0);
        log.info("rangeClosed复数个数：" + evenNum1.count());

        IntStream evenNum2 = IntStream.range(1, 100)
                .filter(n -> n % 2 == 0);
        log.info("range复数个数：" + evenNum2.count());
    }
}
